<?php

class UserController extends Controller {

    private $_model;
    public $activeMenu = 'user';

    /**
     * list all user here
     */
    public function actionIndex() {
        $model = new User('search');
        if (isset($_GET['User'])) {
            $model->attributes = $_GET['User'];
        }

        $this->render('index', compact('model'));
    }

    /**
     * add new member
     */
    public function actionCreate() {
        $model = new User;

        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];

            //check upload avatar
            if (($imageUploadFile = CUploadedFile::getInstance($model, 'photo'))) { // only do if file is really uploaded
                $imageFileName = time() . $imageUploadFile->name;
                $imageUploadFile->saveAs(AVATAR_PATH . $imageFileName);

                //delete previous image
                if ($model->user_avatar != '')
                    @unlink(AVATAR_PATH . $model->user_avatar);

                $model->user_avatar = $imageFileName;
            }

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'Create new user successfully');
                //redirect to update
                $this->redirect(array('update', 'id' => $model->user_id));
            } else {
                Yii::app()->user->setFlash('error', 'Sorry, please try again!');
            }
        }

        $this->render('_form', compact('model'));
    }

    /**
     * update member
     */
    public function actionUpdate() {
        $model = $this->__loadModel();

        //check password
        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];

            $model->validate();

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'Your profile has been updated');
            } else {
                Yii::app()->user->setFlash('error', 'Please check invalid fields');
            }

            $model->user_password = $model->repassword = '';
        }

        $this->render('update', compact('model'));
    }

    public function actionDelete($id) {
        // we only allow deletion via POST request
        $this->__loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->__loadModel($id),
        ));
    }

    protected function __loadModel() {
        if ($this->_model === null) {
            if (isset($_GET['id'])) {
                $this->_model = User::model()->findByPk($_GET['id']);
            }
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }

}